﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;

namespace Enterprise.Management.Common
{
    public class Email
    {
        public static bool Send(string to, string subject, string body, out string msg)
        {
            return Send(string.Empty, to, string.Empty, string.Empty, subject, body, true, out msg);
        }

        public static bool Send(string to, string cc, string subject, string body, out string msg)
        {
            return Send(string.Empty, to, cc, string.Empty, subject, body, true, out msg);
        }

        public static bool Send(string from, string to, string cc, string subject, string body, out string msg)
        {
            return Send(from, to, cc, string.Empty, subject, body, true, out msg);
        }

        public static bool Send(string from, string to, string cc, string bcc, string subject, string body, bool isBodyHtml, out string msg)
        {
            msg = string.Empty;

            SmtpClient smtp = new SmtpClient();
            MailMessage mail = new MailMessage();
            // from
            if (!string.IsNullOrEmpty(from))
            {
                mail.From = new MailAddress(from);
            }
            // to
            string[] tomails = to.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string item in tomails)
            {
                mail.To.Add(new MailAddress(item));
            }
            // cc
            if (!string.IsNullOrEmpty(cc))
            {
                string[] ccmails = cc.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string item in tomails)
                {
                    mail.CC.Add(new MailAddress(item));
                }
            }
            // bcc
            if (!string.IsNullOrEmpty(bcc))
            {
                string[] bccmail = bcc.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string item in tomails)
                {
                    mail.Bcc.Add(new MailAddress(item));
                }
            }

            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = isBodyHtml;

            try
            {
                smtp.Send(mail);
                return true;
            }
            catch (SmtpException smtpException)
            {
                Log.Error(smtpException.Message);

                SmtpStatusCode status = smtpException.StatusCode;
                switch (status)
                { 
                    case SmtpStatusCode.GeneralFailure:
                        msg = string.Format("{0}, can't connect mail server", status, smtp.Host);
                        break;
                    default:
                        msg = string.Format("", (int)status, status, smtp.Host, smtpException.Message);
                        break;
                }
            }
            catch (Exception ex)
            {
                msg = "an unknown error occurred.";
                Log.Error(ex);
            }

            return false;
        }
    }
}